# -*- coding: utf-8 -*-
# created on 2016/11/14

from sympy import simplify, Abs, sympify
from mathsolver.functions.base.base import BaseFunction
from mathsolver.functions.base.objects import BaseFunc, BaseVectorEq, BaseEq, BasePoint
from mathsolver.functions.hanshu.panduan_xiangtong import IsJieXiShiEq
from mathsolver.functions.hanshu.duicheng_jiexishi import ZhongXingDuiChengQiuJieXiShi, ZhouDuiChengQiuJieXiShi


class TuXiangBianHuanQiuJieXiShi(BaseFunction):
    """图像变换求函数解析式"""

    def solver(self, *args):
        func, movement = args[0], args[1]

        # 向量平移
        if isinstance(movement, BaseVectorEq):
            funcname = args[2]
            a, b = sympify(movement.value[1])
            self.output.append(
                BaseFunc({'name': funcname.name, 'var': func.var, 'type': func.type,
                          'expression': simplify(func.expression.subs(func.var, func.var - a) + b)}))
        elif isinstance(movement, BaseEq):
            self.output = ZhouDuiChengQiuJieXiShi().solver(func, movement).output
        elif isinstance(movement, BasePoint):
            self.output = ZhongXingDuiChengQiuJieXiShi().solver(func, movement).output
        elif '平移' in movement:
            number = args[2].sympify()
            if '向左' in movement:
                self.output.append(
                    BaseFunc({'name': func.name, 'var': func.var, 'type': func.type,
                              'expression': simplify(func.expression.subs(func.var, func.var + number))}))
            elif '向右' in movement:
                self.output.append(
                    BaseFunc({'name': func.name, 'var': func.var, 'type': func.type,
                              'expression': simplify(func.expression.subs(func.var, func.var - number))}))
            elif '向上' in movement:
                self.output.append(
                    BaseFunc({'name': func.name, 'var': func.var, 'type': func.type,
                              'expression': func.expression + number}))
            elif '向下' in movement:
                self.output.append(
                    BaseFunc({'name': func.name, 'var': func.var, 'type': func.type,
                              'expression': func.expression - number}))
        elif '对称' in movement:
            if 'x轴' in movement:
                self.output.append(
                    BaseFunc({'name': func.name, 'var': func.var, 'type': func.type,
                              'expression': simplify(-func.expression)}))
            elif 'y轴' in movement:
                self.output.append(
                    BaseFunc({'name': func.name, 'var': func.var, 'type': func.type,
                              'expression': simplify(func.expression.subs(func.var, -func.var))}))
        elif '翻折' in movement:
            if 'x轴' in movement:
                if '向上' in movement:
                    self.output.append(
                        BaseFunc({'name': func.name, 'var': func.var, 'type': func.type,
                                  'expression': simplify(func.expression.subs(func.var, Abs(func.var)))}))
                elif '向下' in movement:
                    self.output.append(
                        BaseFunc({'name': func.name, 'var': func.var, 'type': func.type,
                                  'expression': simplify(func.expression.subs(func.var, -Abs(func.var)))}))
            elif 'y轴' in movement:
                if '向上' in movement:
                    self.output.append(
                        BaseFunc({'name': func.name, 'var': func.var, 'type': func.type,
                                  'expression': simplify(Abs(func.expression))}))
                elif '向下' in movement:
                    self.output.append(
                        BaseFunc({'name': func.name, 'var': func.var, 'type': func.type,
                                  'expression': simplify(-Abs(func.expression))}))
        self.label.add("图像变换求函数解析式")
        return self


class TuXiangBianHuanPanDuan(BaseFunction):
    """判断函数 变换 是否正确
    输入：函数1，变换，函数2
    输出：BaseVariable(True/False)"""

    def solver(self, *args):
        panduan_func = args[-1]
        after_bianhuan_func = TuXiangBianHuanQiuJieXiShi().solver(*args[:-1]).output[0]
        return IsJieXiShiEq().solver(panduan_func, after_bianhuan_func)


if __name__ == '__main__':
    pass
